<?php

namespace App\Http\Controllers\Web;

use App\Driver;
use App\Notifications\FailCheckout;
use App\Notifications\OrderConfirmed;
use App\Order;
use App\Utils\CommonCRUD;
use DB;
use Illuminate\Http\Request;
use Notification;

class OrderController extends Controller
{
    use CommonCRUD;

    protected $_viewGroup = 'web';
    protected $_view = 'order';
    // protected $_orderField = 'no';
    // protected $_order = 'asc';

    protected $_model;
    protected $_where=[];

    public function __construct(\App\Order $model,Request $request)
    {

        $this->_model = $model;
        $this->_where = function($qy) use($request) {
            if($request->input('status')!='') {
                $qy->where('status',$request->input('status'));
            }

            $date = $request->input('date');
            if($date) {
                list($startDate,$endDate) = explode(' - ',$date);
                $endDate .= ' 23:59:59';
                $qy->where('created_at','>=',$startDate)
                    ->where('created_at','<=',$endDate);
            }
            $code = trim($request->input('code'));
            if($code) {
                $qy->where('code',$code);
            }

        };
    }

    public function detail(Request $request) {
        $id = $request->input('id');
        $data['order'] = Order::find($id);
        $data['driver_list'] = Driver::with('line')
            ->where('free','>',0)
            ->get();
        return view('web.order.detail',$data);
    }

    public function checkout(Request $request) {
        //验证
        $this->validate($request, [
            'order_id' => 'required',
            'driver_id' => 'required',
            'amount' => 'required|numeric|min:1',
            'dispatch_at' => 'required|date',
        ]);


        $driver = Driver::find($request->input('driver_id'));
        if(!$driver) {
            return back()->withErrors('司机不存在！');
        }

        if( $driver->free<=0 ) {
            return back()->withErrors('所选司机剩余座位数量不足！');
        }

        $order = Order::find($request->input('order_id'));
        if(!$order || $order->status != 0) {
            return back()->withErrors('订单不存在或状态错误！');
        }

        try {
            DB::beginTransaction();

            $order->amount = $request->input('amount');
            $order->driver_id = $driver->id;
            $order->dispatch_at = $request->input('dispatch_at');
            $order->status = 1;
            $order->save();

            $driver->free = $driver->free - 1;
            $driver->save();

            //发送付款通知
            Notification::send($order->member,new OrderConfirmed($order));

            DB::commit();

            return redirect()
                ->route('admin.order')
                ->with('msg','出票成功！');
        } catch (\Exception $e) {
            DB::rollBack();
            \Log::error('出票失败！'.$e->getMessage());
        }
    }

    public function fail_checkout(Request $request) {
        if($request->isMethod('get')) {
            $id = $request->input('order_id');
            $data['order'] = Order::find($id);
            return view('web.order.fail',$data);
        } else {

            //验证
            $this->validate($request, [
                'order_id' => 'required',
                'fail_msg' => 'required'
            ]);

            $order = Order::find($request->input('order_id'));
            if(!$order || $order->status != 0) {
                return back()->withErrors('订单不存在或状态错误！');
            }

            $order->status = -3;
            $order->fail_msg = $request->input('fail_msg');
            $order->save();

            //推送出票失败通知
            Notification::send($order->member,new FailCheckout($order));

            return redirect()
                ->route('admin.order')
                ->with('msg','操作成功！');
        }
    }

    public function cancel(Request $request) {
        $id = $request->input('id');
        $order = Order::find($id);
        if(!$order || $order->status != 1) {
            return back()->withErrors('订单不存在或状态错误！');
        }

        try {
            DB::beginTransaction();
            $order->status = -1;
            $order->save();

            $driver = $order->driver;
            $driver->free = $driver->free + 1;
            $driver->save();

            DB::commit();
            return redirect()
                ->route('admin.order')
                ->with('msg','操作成功！');
        } catch (\Exception $e) {
            DB::rollBack();
            return back()->withErrors('取消订单失败！'.$e->getMessage());
        }
    }

}
